package com.fmsz.xmly.adapters.classify.tag;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fmsz.xmly.R;
import com.fmsz.xmly.entitys.classify.tag.ClassifyTagEntity;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2016/12/13.
 */

public class TagRvItemAdapter extends RecyclerView.Adapter<TagRvItemAdapter.ViewHolder> {

    private Context context;
    private List<ClassifyTagEntity.CategoryContentsEntity.CategoryContentsList.CategoryContentsListData> datas;
    private RecyclerView recycleView;


    public TagRvItemAdapter(Context context, List<ClassifyTagEntity.CategoryContentsEntity.CategoryContentsList.CategoryContentsListData> datas) {
        this.context = context;
        this.datas = datas;
    }
    public void add(List<ClassifyTagEntity.CategoryContentsEntity.CategoryContentsList.CategoryContentsListData> datas){
        this.datas = datas;
    }


    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        if (this.recycleView!=null) {
            return;
        }
            this.recycleView = recyclerView;
            RecyclerView.ItemDecoration decoration = new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                    super.getItemOffsets(outRect, view, parent, state);
//                if (parent.getChildAdapterPosition(view)!=0 && outRect.top!=2) {
//                    outRect.set(0,2,0,0);
//                }
                    if (parent.getChildAdapterPosition(view) != 0 && TagRvItemAdapter.this.recycleView != null) {
                        outRect.top = 2;
                    }
                }

                @Override
                public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                    super.onDraw(c, parent, state);
                }

                @Override
                public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
                    super.onDrawOver(c, parent, state);
                }
            };
//        recyclerView.invalidateItemDecorations();
//            recyclerView.removeItemDecoration(decoration);
//            this.recycleView.addItemDecoration(decoration);

    }

    @Override
    public void onViewDetachedFromWindow(ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.classify_tag_item_nomal, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.title.setText(datas.get(position).getTitle());
        holder.subTitle.setText(datas.get(position).getSubtitle());
        holder.playsCount.setText(datas.get(position).getPlaysCounts());
        holder.duration_tv.setText(datas.get(position).getTracks() + "集");
        Picasso.with(context).load(datas.get(position).getCoverSmall()).into(holder.icon_rv);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView icon_rv;
        private TextView title;
        private TextView subTitle;
        private TextView duration_tv;
        private TextView playsCount;

        public ViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        private void initView(View itemView) {
            icon_rv = ((ImageView) itemView.findViewById(R.id.icon_tag_lv));
            title = ((TextView) itemView.findViewById(R.id.title_tag_normal));
            subTitle = ((TextView) itemView.findViewById(R.id.subTitle_tag_normal));
            duration_tv = ((TextView) itemView.findViewById(R.id.duration_tag_normal));
            playsCount = ((TextView) itemView.findViewById(R.id.playsCount_tag));
        }
    }
}
